
var app = getApp()
Page({
  data: {
    topCurrentId:'all',
    accountId:'',
    leftCurrentId:'',
  	leftBtn:[],
    topBtn:[],
    list:[],
    page:2,
    backBox:[],
    parent_id:0,
  	goodsType:'',
  	agentId:'',
    selectIndex:'',
    noData:false,
    isVisitor:'',
    isMask:false,
    inviteCode:''
  },
  onLoad: function(options) {
  	app.getMemInfo().then(res=>{
  		this.setData({
  			agentId:res.data.agentId,
        accountId:res.data.id
  		})
      // if (res.data.isVisitor==1) {
      //   this.setData({
      //     isMask:true
      //   })
      // }else{
        this.setData({
          agentId:res.data.agentId
        })
    		this.getFirstTypeList(res.data.agentId)
        this.getGoodsTypeList(res.data.agentId,0)
      // }
  	})
  },
  searchInLibrary(){
    wx.navigateTo({
      url:'searchInLibrary'
    })
  },
  //获取商品类型列表
  getProductType(agentId){
    var that = this
    var leftBtn = this.data.leftBtn
    let data = {
      agentId,
      requestType:'agent'
    }
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      let btnList = res.data.product_types
      //默认种类
      that.setData({
        goodsType:res.data.product_types[0].id
      })
      btnList.forEach(item=>{
        leftBtn.push({name:item.name,id:item.id})
        that.setData({
          leftBtn
        })
      })
      that.getGoodsList()
    })
  },
  selecttypeTap: function(event) {
    var dataSet = event.currentTarget.dataset;
    let type = dataSet.type
    var selectIndex = dataSet.index;
    var categoryId = dataSet.categoryId;//查询不同分类下的数据
    this.setData({
      selectIndex: selectIndex,
      goodsType:type
    })
    this.getGoodsList(type)
  },

  //获取商品列表
  getGoodsList(){
    wx.showLoading({
      title:'正在加载'
    })
    this.setData({
      list:[]
    })
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      pageNum:1,
      searchKey:''
    }
    console.log(data)
    let list = []
    app.request('wx_app/stock_all/sku/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {      
        res.data.pageContent.forEach(item=>{
          list.push({
            name:item.product_name,
            price:item.price,
            url:item.main_img_url,
            id:item.id,
            type:item.product_type,
            isAdded:item.isAdded,
            companyName:item.productTag,
            color:item.tagColor
          })
          that.setData({
            list
          })
        })
        console.log(that.data.list.length)
        wx.hideLoading()
        if (that.data.list.length==0) {
          that.setData({
            noData:true
          })
        }else{
          that.setData({
            noData:false
          })
        }
      }else{
        that.setData({
            noData:true
          })
        wx.hideLoading()
        res.errorMsg(res.message)
      }
    })

  },
  bindMore(){
    wx.showLoading({
      title:'正在加载'
    })
    let page = this.data.page
    var that = this
    var data = {
      pageNum:page,
      agentId:this.data.agentId,
      type:this.data.goodsType,
      searchKey:this.data.searchKey,
    }
    let list = this.data.list
    console.log(data)
    app.request('wx_app/stock_all/sku/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {      
        res.data.pageContent.forEach(item=>{
          list.push({
            name:item.product_name,
            price:item.price,
            url:item.main_img_url,
            id:item.id,
            type:item.product_type,
            isAdded:item.isAdded,
            companyName:item.productTag,
            color:item.tagColor
          })
          that.setData({
            list
          })
        })
        wx.hideLoading()
      }else{
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
    page++
    this.setData({
      page:page
    })
  },
  //获取一级分类
  getFirstTypeList(agentId){
    const that = this
    let topBtn = this.data.topBtn
    let data = {
      agentId,
      requestType:'agent'
    }
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      res.data.product_types.forEach(item=>{
        if (item.parent_id==0&&item.id!=98) {
          topBtn.push({name:item.name,id:item.id})
        }
      })
      that.setData({
        topBtn
      })
    })
  },
  chooseAll(){
    this.setData({
      topCurrentId:'all',
      parent_id:0,
      leftCurrentId:0,
      page:2,
    })
    this.getGoodsTypeList(this.data.agentId,0)
  },
  chooseFirstType(e){
    
    let id = e.currentTarget.dataset.index
    let type = e.currentTarget.dataset.type
    this.setData({
      topCurrentId:id,
      parent_id:type,
      selectIndex:0,
      page:2,
    })
    this.getGoodsTypeList(this.data.agentId,type)
  },
  //获取商品种类
  getGoodsTypeList(agentId,type){
    const that = this
    let leftBtn = []
    let backBox = []
    let goodsType=''
    let data = {
      agentId,
      requestType:'agent'
    }
    console.log(type)
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      res.data.product_types.forEach(item=>{
        if (type==item.parent_id&&type!=0) {
          leftBtn.push({name:item.name,id:item.id})
        }else if (type==0) { //全部
          if (item.parent_id!=0) {
            leftBtn.push({name:item.name,id:item.id})
          }
        }
        if (type==item.id) { 
          backBox.push({name:item.name,id:item.id})
        }
      })
      that.setData({
        leftBtn,
        backBox
      })
      if (that.data.leftBtn==''&&that.data.topCurrentId=='all') {
        res.data.product_types.forEach(item=>{
          leftBtn.push({name:item.name,id:item.id})
        })
        that.setData({
          leftBtn
        })
      }
      if (that.data.leftBtn=='') {
        that.setData({
          leftBtn:that.data.backBox
        })
      }
      goodsType = that.data.leftBtn[0].id
      that.setData({
        goodsType
      })
      that.getGoodsList()
    })
  },
  //添加入库
  addToStock(e){
    var that = this
    let data = {
      accountId:this.data.accountId,
      agentId:this.data.agentId,
      stockSkuAllId:e.currentTarget.dataset.id //商品ID
    }
    console.log(data)
    app.request('wx_app/agent/stock/add',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        that.getGoodsList()
        that.setData({
          page:2
        })
      }
    })
  },
  getInviteCode(e){
    this.setData({
      inviteCode:e.detail.value
    })
  },
  bindCancel(){
    wx.navigateBack({
      delta: 1
    })
  },
  bindConfirm(){
    let that = this
    let data = {
      accountId:this.data.accountId,
      inviteCode:this.data.inviteCode
    }
    console.log(data)
    app.request('wx_app/agent_account/input_inviteCode',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        wx.setStorage({
          key:'memInfo',
          data:{
            agentId:res.data.agentId,
            id:res.data.id==null?0:res.data.id,
            gender:res.data.gender,
            username:res.data.username,
            name:res.data.name,
            role:res.data.role,
            isEnable:res.data.isEnable,
            shop:res.data.agentCompanyName,
            isVisitor:res.data.isVisitor,
            vipUserName:res.data.vipUserName,
            phone:res.data.phone,
            vipName:res.data.vipName,
            vipId:res.data.vipId
          },
          success(item){
            wx.showToast({
              title:'认证成功'
            })
            that.setData({
              isMask:false
            })
            that.getFirstTypeList(res.data.agentId)
            that.getGoodsTypeList(res.data.agentId,0)
          },
          fail(item){
            app.errorMsg('您输入的邀请码有误')
          }
        })
      }else{
        app.errorMsg('您输入的邀请码有误')
      }
    })
  },
})